<?php
/**
 * 商品模型
 * User: ws
 * Date: 2018/7/24
 * Time: 11:12
 */

namespace app\merchant\model;
use think\Model;

class Goods extends Model
{
    function initialize()
    {
        parent::initialize();
    }
    protected $autoWriteTimestamp = true;

    function add($params){
        $result = $this->isUpdate(false)->setAttr('goods_id',null)->allowField(true)->save($params);
        if($result){
            return $this->goods_id;
        }else{
            return false;
        }
    }



    function edit($params){
        $result = $this->isUpdate(true)->allowField(true)->save($params);
        if($result){
            return true;
        }else{
            return false;
        }
    }

    function store()
    {
        //return $this->belongsTo('GoodsType');
        return $this->hasOne('store','store_id','store_id');
    }

     function goodsType()
    {
        //return $this->belongsTo('GoodsType');
        return $this->hasOne('GoodsType','type_id')->field('type_name');
    }

    /**
     * 批量操作
     * $act  操作类型 delete
     * $params 参数
     */
    function batches($act,$params){
        if($act == 'delete'){
            $result = $this->destroy($params);
        }
        if($result){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 获取图片列表
     * $whith_page 0不分页  1分页返回对象  2分页返回数组
     *
     */
    function get_list($where = '',$order = 'goods_sort desc',$page_size=15,$whith_page=0){
        if(!$order){ $order = 'goods_sort desc';}
        if($whith_page == 0){
            $articles = $this->where($where)->order($order)->limit($page_size)->select();
            return $articles;
        }
        $articles = $this->where($where)->order($order)->paginate($page_size);
        if($whith_page == 1){
            return $articles;
        }
        $articles = $articles->toArray();
        return $articles;
    }
}